package com.morder.activity;

import com.morder.R;
import com.morder.application.AppPreference;
import com.morder.application.BuildConfig;
import com.morder.provider.LoginTask;
import com.morder.provider.LoginTask.LoginTaskListener;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.os.AsyncTask;

public class LoginActivity extends Activity implements OnClickListener {

  private TextView mLogin;
  private TextView mRegister;
  private BroadcastReceiver mReceiver;
  private EditText mNum;
  private EditText mPwd;
  private ProgressDialog mProgressDlg;
  private LoginTask mLoginTask;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login_activity);

    mLogin = (TextView) findViewById(R.id.login);
    mRegister = (TextView) findViewById(R.id.register);

    mNum = (EditText) findViewById(R.id.accounts);
    mPwd = (EditText) findViewById(R.id.password);

    mLogin.setOnClickListener(this);
    mRegister.setOnClickListener(this);

    interfaceRefresh();
    registerReceiver(mReceiver, new IntentFilter(BuildConfig.REGISTER_SUCCESS));
  }

  private void interfaceRefresh() {
    mReceiver = new BroadcastReceiver() {

      @Override
      public void onReceive(Context context, Intent intent) {
        String msg = intent.getStringExtra("msg");
        if (msg.equals("close")) {
          finish();
        }
      }
    };
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    registerReceiver(mReceiver, new IntentFilter(BuildConfig.REGISTER_SUCCESS));
    unregisterReceiver(mReceiver);
  }

  private void cancelLoginTask() {
    if ((mLoginTask != null) && (mLoginTask.getStatus() == AsyncTask.Status.RUNNING)) {
      mLoginTask.cancel(true);
      mLoginTask = null;
    }
  }

  @Override
  public void onClick(View v) {
    switch (v.getId()) {
    case R.id.login:
      final String number = mNum.getText().toString();
      final String password = mPwd.getText().toString();
      mProgressDlg = new ProgressDialog(LoginActivity.this);
      mProgressDlg.setProgressStyle(ProgressDialog.STYLE_SPINNER);
      mProgressDlg.setMessage("login...");
      mProgressDlg.setCancelable(true);
      mProgressDlg.setOnCancelListener(new OnCancelListener() {

        @Override
        public void onCancel(DialogInterface dialog) {
          cancelLoginTask();
        }
      });
      mProgressDlg.show();
      mLoginTask = new LoginTask(LoginActivity.this).execute(number, password, new LoginTaskListener() {

        @Override
        public void onLoginFinish(boolean success, String reason) {
          if (success) {
            mProgressDlg.dismiss();
            AppPreference.markLoginSuccess(true);
            boolean isFirstBootFinish = AppPreference.isFirstBootFinished();
            System.out.println("isFirstBootFinish: " + isFirstBootFinish);
            if (isFirstBootFinish) {
              Intent intent = new Intent(LoginActivity.this, NavigationActivity.class);
              startActivity(intent);
              finish();
            } else {
              Intent intent = new Intent(LoginActivity.this, MainActivity.class);
              startActivity(intent);
              finish();
            }
          } else {
            mProgressDlg.dismiss();
            new AlertDialog.Builder(LoginActivity.this).setTitle("登录失败").setMessage(reason)
                .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                  
                  @Override
                  public void onClick(DialogInterface dialog, int which) {
                    Intent intent = new Intent(LoginActivity.this, NavigationActivity.class);
                    startActivity(intent);
                    finish();
                  }
                }).create().show();
          }
        }
      });
      break;
    case R.id.register:
      Intent register = new Intent(this, RegisterActivity.class);
      startActivity(register);
      break;
    default:
      break;
    }
  }

}
